﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Appcode;



public partial class Site : System.Web.UI.MasterPage
{
    private ProductsDAL objProd = new ProductsDAL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            isLogin();

            if (Session.GetLoginUser() != null)
            {
                List<Images> listImg = objProd.getAllImagesByUID(Session.GetLoginUser().UserID);

                if (listImg != null)
                {
                    picStatus.InnerText = listImg.Count + " pics";
                }
                else
                {
                    picStatus.InnerText = " 0 pics";
                }
            }


        }
    }
    protected void FastLogin(object sender, EventArgs e)
    {
        LoginToSystem(txtUser.Text, txtPassWord.Text);
        Response.Redirect("Default.aspx");
    }

    public void isLogin()
    {

        HttpCookie reg = Request.Cookies["regUser"];

        if (reg != null)
        {
            String regAcc = reg["txtacc"].ToString();
            String regPass = reg["txtpass"].ToString();

            LoginToSystem(regAcc, regPass);

        }

        if (Session.GetLoginUser() == null)
        {
            loginForm.Visible = true;
            managerForm.Visible = false;
            lgmnu.Visible = true;
            regismnu.Visible = true;
        }
        else
        {
            lblName.Text = Session.GetLoginUser().UserName;
            loginForm.Visible = false;
            managerForm.Visible = true;
            lgmnu.Visible = false;
            regismnu.Visible = false;
        }
    }

    public void LoginToSystem(String regAcc, String regPass)
    {
        UsersDAL uDAL = new UsersDAL();
        UserLogin uLog = uDAL.CheckLogin(regAcc, regPass);
        if (uLog != null && uLog.UserName != null)
        {
            //Set session:
            Session.SetLoginUser(uLog);

            //set cookies
            if (rmbchkbox.Checked == true)
            {
                HttpCookie reg = new HttpCookie("regUser");
                reg["txtacc"] = regAcc;
                reg["txtpass"] = regPass;
                reg.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(reg);
            }

        }
    }



}
